package com.projectbox.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.projectbox.model.Project;
import com.projectbox.model.User;
import com.projectbox.service.ProjectService;

@Controller
@RequestMapping("/api/project")
public class ProjectAPIController {

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	private ProjectService projectService;

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}
	
	@RequestMapping(value="/myproject", method = RequestMethod.GET)
	@ResponseBody
	public List<Project> getMyProject(@RequestParam(value = "uid", required=true) Long uid) {
		logger.debug("getMyProject gets call");
		
		User user = new User();
		user.setId(uid);
		List<Project> projectList = this.projectService.findManagedProjects(user);	
		return projectList;
	}
	
	@RequestMapping(value="/contributor", method = RequestMethod.GET)
	@ResponseBody
	public List<User> getMyProjectContributor(@RequestParam(value = "pid", required=true) Long pid) {
		logger.debug("getMyProjectContributor gets call");
		
		List<User> userList = this.projectService.findProjectContributor(pid);
		return userList;
	}
	
	@RequestMapping(value="/delete", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> deleteProject(@RequestParam(value = "pid", required=true) Long pid,
			@RequestParam(value = "cid", required=false) Long cid) {
		
		logger.debug("deleteProject gets call");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		
		boolean result = false;
		
		if (cid != null) {
			result = this.projectService.removeProjectContributor(pid, cid);
			map.put("success", result);
		} else {
			result = this.projectService.deleteProject(pid);
			map.put("success", result);
		}
		
		return map;
	}
	
	
	@RequestMapping(value="/newproject", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> processNewProjectSubmission(@RequestParam(value = "projectname", required = true) String name,
			@RequestParam(value = "description", required = false) String description,
			@RequestParam(value = "private", required = false) Boolean isPrivate,
			@RequestParam(value = "uid", required = true) Long uid,
			Model model) {
		
		logger.debug(name + " " + description + " " + isPrivate);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		
		Project project = this.projectService.findProject(name);
		if (project != null) {
			return map;
		}
		
		if (isPrivate != null) {
			projectService.addNewProject(name, description, isPrivate, uid);
		} else {
			projectService.addNewProject(name, description, false, uid);
		}

		map.put("success", true);
		return map;
	}
}
